package com.annoncesautomobile.vue;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.util.ModelXML;
import com.annoncesautomobile.vue.adapters.ListeBoutonBas;

public class SuperActivity extends Activity 
{
    protected ProgressDialog progressDialog;
    
	protected void addBoutonBas()
	{
		ListeBoutonBas listeBoutonBas = new ListeBoutonBas(this);
		LinearLayout lt = (LinearLayout) this.findViewById(R.id.mainLayout);
		lt.addView(listeBoutonBas.getView());
	}
	
	public boolean isInternetAvailable = true;

	public boolean isOnline() {
		try
		{
			ConnectivityManager cm =
					(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netInfo = cm.getActiveNetworkInfo();
			if (netInfo != null && netInfo.isConnectedOrConnecting()){
				return true;
			}
		}
		catch(Exception e)
		{
			return false;
		}
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ModelXML modele = ModelXML.getInstance();
		try
		{
			if(isOnline())
			{
				modele.getCategories();
				modele.getDernieresAnnonces();
				modele.getMarques();
			}
			else
			{
				Intent intent = new Intent(SuperActivity.this, SplashActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}
		catch(RuntimeException ex)
		{
			Intent intent = new Intent(SuperActivity.this, SplashActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		catch (Exception e) {
			//Add Log here
			Log.e("AnnonceAutomobile", "Uncaught exception is: ", e);
		}
		
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		ModelXML modele = ModelXML.getInstance();
		try
		{
			if(isOnline())
			{
				modele.getCategories();
				modele.getDernieresAnnonces();
				modele.getMarques();
			}
			else
			{
				Intent intent = new Intent(SuperActivity.this, SplashActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}
		catch(RuntimeException ex)
		{
			Intent intent = new Intent(SuperActivity.this, SplashActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		catch (Exception e) {
			//Add Log here
			Log.e("AnnonceAutomobile", "Uncaught exception is: ", e);
		}
	}
}
